﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class BeiPin_CaiGou_JiHua : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = @"SELECT [生产单位ID]
                    ,[填报日期]
                    ,[单据编号]      
                     FROM [SCM].[dbo].[备品备件采购计划] where [ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.TianBaoDanWei.DataBind();
                        //        this.CaiGouZhuGuan.DataBind();
                        //        this.CaiGouYuan.DataBind();

                        //        this.CangChuZhuGuan.DataBind();
                        //        this.KuGuanYuan.DataBind();

                        this.ViewState["BillID"] = BillID;
                        this.TianBaoDanWei.Text = Reader.GetInt32(0).ToString();
                        this.RiQi.Text = Reader.GetDateTime(1).ToString("yyyy年M月d日");
                        this.DanHao.Text = Reader.GetString(2).Trim();
                        this.DanHao.ReadOnly = true;
                        Reader.Close();
                        Cmd.CommandText = @"select 备品备件采购明细.[备品备件ID],[备品备件].[品名] as [名称],
                               [备品备件].[规格],[备品备件].单位,备品备件采购明细.数量,
                               [备品备件].单价,备品备件采购明细.备品备件供货单位ID,备品备件采购明细.用途
                               from 备品备件采购明细,[备品备件] where 备品备件采购明细.备品备件ID=[备品备件].ID and 备品备件采购明细.备品备件采购计划ID=" + BillID;
                        Reader = Cmd.ExecuteReader();
                        for (int i = 1; i <= 5; i++)
                        {
                            if (!Reader.Read())
                            {
                                break;
                            }
                            this.ViewState["id" + i] = Reader.GetInt32(0).ToString();
                            ((TextBox)this.FindControl("R" + i + "_C1")).Text = Reader.GetString(1);
                            ((TextBox)this.FindControl("R" + i + "_C2")).Text = Reader.GetString(2);
                            ((TextBox)this.FindControl("R" + i + "_C3")).Text = Reader.GetString(3);
                            ((TextBox)this.FindControl("R" + i + "_C4")).Text = Reader.GetInt32(4).ToString();
                            float Amount;
                            float Price;
                            Amount = Reader.GetInt32(4);
                            Price = Reader.GetFloat(5);
                            ((TextBox)this.FindControl("R" + i + "_C5")).Text = Price.ToString();
                            ((TextBox)this.FindControl("R" + i + "_C6")).Text = (Amount * Price).ToString(".00");
                            int j = Reader.GetInt32(6);
                            ((DropDownList)this.FindControl("DropDownList" + i)).Visible = true;
                            ((DropDownList)this.FindControl("DropDownList" + i)).Text = Reader.GetInt32(6).ToString();
                            ((TextBox)this.FindControl("R" + i + "_C4")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C4")).CssClass = "intxt1";
                            ((TextBox)this.FindControl("R" + i + "_C8")).Text = Reader.GetString(7);
                            ((TextBox)this.FindControl("R" + i + "_C8")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C8")).CssClass = "intxt1";
                            ((Button)this.FindControl("Button_Add" + i)).Text = "删 除";
                        }
                        Reader.Close();
                    }
                    else
                    {
                        Reader.Close();
                    }

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
            }
        }
        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·已取消添加";
        }
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            int row;
            int.TryParse(e.CommandArgument.ToString(), out row);
            this.ViewState["row"] = row;

            if (this.ViewState["id" + row] == null)
            {
                this.Panel_Grid.Visible = true;
                this.Message.Text = "·准备添加";
            }
            else
            {
                this.ViewState["id" + row] = null;
                ((TextBox)this.FindControl("R" + row + "_C1")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C2")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C8")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = true;

                ((DropDownList)this.FindControl("DropDownList" + row)).Visible = false;             
                ((Button)this.FindControl("Button_Add" + row)).Text = "添 加";
                this.Message.Text = "·删除成功";
            }

        }
        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            int row = (int)this.ViewState["row"];
            string[] Args = e.CommandArgument.ToString().Split("\t".ToCharArray());

            this.ViewState["id" + row] = Args[0];
            ((TextBox)this.FindControl("R" + row + "_C1")).Text = Args[1];
            ((TextBox)this.FindControl("R" + row + "_C2")).Text = Args[2];
            ((TextBox)this.FindControl("R" + row + "_C3")).Text = Args[3];
            ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "intxt1";       
            ((TextBox)this.FindControl("R" + row + "_C5")).Text = Args[4];
            ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = true;
            ((DropDownList)this.FindControl("DropDownList" + row)).Visible = true;    
            ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "intxt1";
        

            ((Button)this.FindControl("Button_Add" + row)).Text = "删 除";
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·添加成功";
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("YuanLiao_CaiGou_JiHua.aspx");
        }

        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            float f1, f2;
            this.Message.Text = "";
            for (int i = 1; i <= 8; i++)
            {
                if (this.ViewState["id" + i] != null)
                {
                    f1 = 0;
                    f2 = 0;
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C4")).Text, out f1);
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C5")).Text, out f2);
                    if (f1 * f2 <= 0)
                    {
                        ((TextBox)this.FindControl("R" + i + "_C6")).Text = "";
                        this.Message.Text += " ·第" + i + "行数量或者单价输入有误";
                    }
                    else
                    {
                        ((TextBox)this.FindControl("R" + i + "_C6")).Text = (f1 * f2).ToString(".00");
                    }
                }

            }

            if (this.Message.Text == "")
            {
                this.Message.Text = "·完成金额计算";
            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            Button_Calc_Click(null, null);
            if (this.Message.Text != "·完成金额计算") return;

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "BeiPin_CaiGou_JiHua";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@生产单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.TianBaoDanWei.Text));
                Cmd.Parameters.Add(new SqlParameter("@填报日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.RiQi.Text)));
                Cmd.Parameters.Add(new SqlParameter("@单据编号", SqlDbType.NChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.DanHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                Cmd.ExecuteNonQuery();
                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    Cmd.CommandText = "BeiPin_CaiGou_JiHua_MingXi";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Clear();
                    Cmd.Parameters.Add(new SqlParameter("@备品备件采购计划ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, BillID));
                    Cmd.Parameters.Add(new SqlParameter("@备品备件ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@数量", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@备品备件供货单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@用途", SqlDbType.NVarChar, 150, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,null));
                    for (int i = 1; i <= 15; i++)
                    {
                        if ((string)ViewState["id" + i] != null)
                        {
                            Cmd.Parameters["@备品备件ID"].Value = int.Parse((string)ViewState["id" + i]);
                            Cmd.Parameters["@数量"].Value = float.Parse(((TextBox)this.FindControl("R" + i + "_C4")).Text);

                            Cmd.Parameters["@备品备件供货单位ID"].Value = int.Parse(((DropDownList)this.FindControl("DropDownList" + i)).Text);
                            Cmd.Parameters["@用途"].Value = ((TextBox)this.FindControl("R" + i + "_C8")).Text;
                            Cmd.ExecuteNonQuery();
                        }
                    }
                    this.Message.Text += "·保存成功";
                    this.DanHao.ReadOnly = true;
                }
               else
                {
                    this.Message.Text = "·单价编号(No.)已经存在";
               }

                Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();
         
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("BeiPin_CaiGou_JiHua_Search.aspx");
        }

    }
}
